﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.IO;

namespace Expressions.Compiler.Internal
{
    /// <summary>
    /// Reader implementation
    /// </summary>
    internal class Reader<T>
    {
        private IEnumerator<T> _reader;
        private T _value;

        /// <summary>
        /// Initializes a new instance of the <see cref="Reader"/> class.
        /// </summary>
        /// <param name="input">The input.</param>
        internal Reader(IEnumerator<T> reader)
        {
            _reader = reader;
        }

        /// <summary>
        /// Reads this instance.
        /// </summary>
        /// <returns></returns>
        internal void Read()
        {
            if (_reader.MoveNext())
            {
                _value = _reader.Current;
            }
            else
            {
                _value = Terminal<T>.Default;
            }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <value>The value.</value>
        internal T Value { get { return _value; } }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        internal void Reset()
        {
            _reader.Reset();
        }
    }

    /// <summary>
    /// Reader implementation
    /// </summary>
    internal class Reader
    {
        private TextReader _reader;
        private char _ch;

        /// <summary>
        /// Initializes a new instance of the <see cref="Reader"/> class.
        /// </summary>
        /// <param name="input">The input.</param>
        internal Reader(string input)
        {
            _reader = new StringReader(input);
        }

        /// <summary>
        /// Reads this instance.
        /// </summary>
        /// <returns></returns>
        internal void Read()
        {
            _ch = (char)_reader.Read();
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <value>The value.</value>
        internal char Value { get { return _ch; } }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        internal void Close()
        {
            _reader.Close();
        }
    }
}
